<!--分类管理-->
<template>
	<el-card>
		<el-form :inline="true" :model="state.queryForm" @keyup.enter="getDataList()">
			<el-form-item class="form-button-right">
				<el-button v-auth="'module:position/type:save'" type="primary" @click="addOrUpdateHandle()">新增</el-button>
				<el-button v-auth="'module:position/type:delete'" type="danger" @click="deleteBatchHandle()">删除</el-button>
				<el-button type="danger" @click="toggleExpandAll()">
					<el-icon style="width: 100%"><Sort />展开/折叠</el-icon>
				</el-button>
			</el-form-item>

			<el-form-item>
				<KeyQuery v-model="state.queryForm.keyword" @getDataList="getDataList()"/>
			</el-form-item>
		</el-form>

		<el-table
			v-if="refreshTable"
			v-loading="state.dataListLoading"
			:default-expand-all="isExpandAll"
			:data="state.dataList"
			row-key="id"
			border
			style="width: 100%"
			@selection-change="selectionChangeHandle"
		>
			<el-table-column type="selection" header-align="center" align="center" width="50"></el-table-column>

			<el-table-column width="200" prop="name" label="组织名称" header-align="center" align="center"></el-table-column>
			<el-table-column prop="remark" label="描述" header-align="center" align="center"></el-table-column>
			<el-table-column label="操作" fixed="right" header-align="center" align="center" width="250">
				<template #default="scope">
					<el-button v-auth="'module:organization:update'" type="primary" link @click="addSubset(scope.row.id)">添加下级</el-button>
					<el-button v-auth="'module:position/type:update'" type="primary" link @click="addOrUpdateHandle(scope.row.id)">编辑</el-button>
					<el-button v-auth="'module:position/type:delete'" type="primary" link @click="deleteBatchHandle(scope.row.id)">删除</el-button>
				</template>
			</el-table-column>
		</el-table>

		<!-- 弹窗, 新增 / 修改 -->
		<add-or-update ref="addOrUpdateRef" @refreshDataList="getDataList"></add-or-update>
	</el-card>
</template>

<script setup lang="ts" name="ModulePositionTypeIndex">
import { useCrud } from '@/hooks'
import { reactive, ref, nextTick, toRaw } from 'vue'
import { IHooksOptions } from '@/hooks/interface'
import AddOrUpdate from './plane/type-a-u.vue'
import { findByCode } from '@/api/nb/business/dictionaries'

const state: IHooksOptions = reactive({
	dataListUrl: '/nb/business/dictionaries_subset/list',
	deleteUrl: '/nb/business/dictionaries_subset/delete',
	queryForm: {
		code: 'PeiXunJiHua'
	},
	isPage: false
})

//新增页面
const addOrUpdateRef = ref()
const addOrUpdateHandle = (id?: number) => {
	addOrUpdateRef.value.dataPList = fnPDataList()
	addOrUpdateRef.value.init(id)
	addOrUpdateRef.value.dataForm.pid = '0'
}

//添加下级页面
const addSubset = (id?: number) => {
	addOrUpdateRef.value.dataPList = fnPDataList()
	addOrUpdateRef.value.init()
	addOrUpdateRef.value.dataForm.pid = id
}

const fnPDataList = () => {
	var aa: any[] = []
	Object.assign(aa, state.dataList)
	aa?.unshift({
		id: '0',
		name: '一级类型'
	})
	return aa
}

// 是否展开，默认全部折叠
const isExpandAll = ref(true)
// 是否重新渲染表格状态
const refreshTable = ref(true)

/**
 * 切换 展开/折叠
 */
const toggleExpandAll = () => {
	refreshTable.value = false
	isExpandAll.value = !isExpandAll.value
	nextTick(() => {
		refreshTable.value = true
	})
}

//处理pid
const fnDictId = (code: string) => {
	return findByCode(code).then(res => {
		addOrUpdateRef.value.dataForm.dictId = res.data.id
	})
}

fnDictId('PeiXunJiHua')

const { getDataList, selectionChangeHandle, deleteBatchHandle } = useCrud(state)
</script>
